# -*- coding: utf-8 -*-
import maya.cmds as cmds
import maya.mel as mel
import sys
#reload(sys)
#sys.setdefaultencoding('utf-8')
class Check_Export_Cam_FBX():
	def Check_Export_Cam_FBX(self,Exp_Path):
		file_path = cmds.file (q = True , sceneName = True)
		file_name = file_path.split ('/')[-1]
		file_name = file_name.split ('.')
		ListCam_list = [i for i in cmds.listCameras (p = 1) if i != "persp"]
		ListCam = ListCam_list[0]
		Old_Cam = ''
		if cmds.nodeType (ListCam) == "camera" :
			Old_Cam = cmds.listRelatives (ListCam , p = True)[0]
		else :
			Old_Cam = ListCam
		if len (ListCam_list) == 1 :
			try:
				New_CamName = Old_Cam + "_OUT"
				New_Cam = cmds.rename (cmds.camera ()[0] , New_CamName)
				cmds.parentConstraint (Old_Cam , New_Cam , name = New_Cam + '_Constraint')
				New_Cam_shape = cmds.listRelatives (New_Cam , shapes = True , children = True)[0]
				Old_Cam_shape = cmds.listRelatives (Old_Cam , shapes = True , children = True)[0]
				cmds.connectAttr (Old_Cam_shape + ".focalLength" , New_Cam_shape + ".focalLength")
				cmds.connectAttr (Old_Cam_shape + ".filmFit" , New_Cam_shape + ".filmFit")
				cmds.connectAttr (Old_Cam_shape + ".horizontalFilmAperture" , New_Cam_shape + ".horizontalFilmAperture")
				cmds.connectAttr (Old_Cam_shape + ".verticalFilmAperture" , New_Cam_shape + ".verticalFilmAperture")
				cmds.connectAttr (Old_Cam_shape + ".lensSqueezeRatio" , New_Cam_shape + ".lensSqueezeRatio")
				cmds.connectAttr (Old_Cam_shape + ".fStop" , New_Cam_shape + ".fStop")
				cmds.connectAttr (Old_Cam_shape + ".focusDistance" , New_Cam_shape + ".focusDistance")
				cmds.connectAttr (Old_Cam_shape + ".shutterAngle" , New_Cam_shape + ".shutterAngle")
				cmds.connectAttr (Old_Cam_shape + ".centerOfInterest" , New_Cam_shape + ".centerOfInterest")

				st = cmds.playbackOptions (min = 1 , q = 1)
				et = cmds.playbackOptions (max = 1 , q = 1)
				cmds.select (New_Cam , replace = True)
				cmds.bakeResults (New_Cam , t = (st , et) , sb = 1)
				cmds.setAttr (New_Cam + ".tx" , lock = 1)
				cmds.setAttr (New_Cam + ".ty" , lock = 1)
				cmds.setAttr (New_Cam + ".tz" , lock = 1)
				cmds.setAttr (New_Cam + ".rx" , lock = 1)
				cmds.setAttr (New_Cam + ".ry" , lock = 1)
				cmds.setAttr (New_Cam + ".rz" , lock = 1)
				cmds.setAttr (New_Cam + ".sx" , lock = 1)
				cmds.setAttr (New_Cam + ".sy" , lock = 1)
				cmds.setAttr (New_Cam + ".sz" , lock = 1)
				cmds.setAttr (New_Cam + ".v" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".hfa" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".vfa" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".fl" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".lsr" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".fs" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".fd" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".sa" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".coi" , lock = 1)
				cmds.setAttr (New_Cam_shape + ".lls" , lock = 1)
				Export_Path = Exp_Path.replace ('\\' , '/') + '/' + file_name[0] + '_cam.fbx'
				mel.eval ("FBXResetExport; "
						  "FBXExportInputConnections -v false; "
						  "FBXExportBakeComplexAnimation -v false; "
						  "FBXExportLights -v false; "
						  "FBXExportCameras -v true; "
						  "FBXExportInAscii -v true;"
						  "FBXExportFileVersion FBX201700;"
						  "FBXExportSmoothingGroups -v true; "
						  "FBXExportSmoothMesh -v true;"
						  "FBXExportApplyConstantKeyReducer -v false;"
						  "FBXExportBakeComplexAnimation -v true; "
						  "FBXExportBakeComplexStep -v true; "
						  "FBXExportCameras -v true;")
				mel.eval (('FBXExport -f \"{}\" -s').format (Export_Path))
				cmds.delete (cmds.ls (New_Cam))
				return ("YES")
			except :
				return ("NO-未知错误-手动检查文件")
		else :
			return ("NO-存在多余相机导出失败"+str(ListCam_list))